<?php
#
#
# Copyright TEDECO, 2009. http://tedeco.fi.upm.es
#
# This file is part of kumenya.
#
# Kumenya is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Kumenya is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
?>
<?php
	Interfaz::CheckSession(Accountant);

	Interfaz::Record();

	$multipleSearch = FALSE;
	// ACCOUNTS RANGE
	$endingAccount = Interfaz::Data(EndingAccountingAccountUid);
	if ($endingAccount) {
		$multipleSearch = TRUE;
		$accounts = AccountingAccount::SearchInRange(Interfaz::Data());
		
		if ($accounts) {
			$table = array();
			$total = array();
			$accountData = array();
			foreach ($accounts as $account) {
				$Uidaccount = $account[Uid];
				$accountType = $account[ThirdType];
				$criterias = array_merge (array(Uid . AccountingAccountsTable => $Uidaccount), Interfaz::Data());
				
				$accountData[$Uidaccount] = AccountingAccount::SeeDetails($accountType, $account);

				if ($accountData[$Uidaccount]) {
					$accountData[$Uidaccount][StartDate] = Interfaz::Data(StartDate);
					$accountData[$Uidaccount][EndDate] = Interfaz::Data(EndDate);
					
					$table[$Uidaccount] = AccountingAccount::LedgerBalance ($criterias);
				
					if (is_array($table[$Uidaccount])) {
						$total[$Uidaccount] = AccountingAccount::LedgerBalanceTotal ($criterias);
						if (is_array($total[$Uidaccount]))
							continue;
						else {
							if (!Interfaz::Error())
								Interfaz::Error(_("Unable to retrieve the accounting account"));
							$result = FALSE;
							break;
						}
					}
					else {
						if (!Interfaz::Error())
							Interfaz::Error(_("Unable to retrieve the accounting account"));
						$result = FALSE;
						break;
					}
				}
				else {
					if (!Interfaz::Error())
						Interfaz::Error(_("Unable to retrieve the accounting account"));
					$result = FALSE;
					break;
				}
			}
			include "LedgerBalanceExcelResult.php";
			$result = TRUE;			
		}
		else {
			if (!Interfaz::Error())
				Interfaz::Error(_("Unable to retrieve the accounting account"));
			$result = FALSE;
		}
	}
	else {
		$criterias = array_merge (array(Uid . AccountingAccountsTable => 
			Interfaz::Data(openingAccountungAccountUid)), Interfaz::Data());
		$table = AccountingAccount::LedgerBalance ($criterias);	
	
		if (is_array($table)) {
			$total = AccountingAccount::LedgerBalanceTotal ($criterias);
			
			if (is_array($table)) {
				include "LedgerBalanceExcelResult.php";
				$result = TRUE;			
			}
			else
				$result = FALSE;
		}
		else
			$result = FALSE;
	}
?>